// pages/shopping/shopCar.js
import {
    store
} from '../../api/apiData'
var pageNum1 = 1; //页码
Page({

    /**
     * 页面的初始数据
     */
    data: {
        result: ['a', 'b'],
        loadMoreText: '加载中...', //底部文字
        list: [],
        ids_list: [],
        delStatus: false,
        mergedArr: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getList(1)

    },
    checkboxChange(e) {
        let list = this.data.list
        let val = e.detail.value
        let objArr = []
        val.forEach(item => {
            list.forEach(item2 => {
                item2.list.forEach(item3 => {
                    if (item == item3.product_sku_id) {
                        objArr.push({
                            list: item3,
                            merchant: item2.merchant
                        })
                    }

                })
            })
        })
        const mergedArr = objArr.reduce((acc, obj) => {
            const foundObj = acc.find(item => item.merchant === obj.merchant);
            if (foundObj) {
                foundObj.list.push(obj.list); // 将相同属性的 id 添加到 ids 数组中
            } else {
                acc.push({
                    merchant: obj.merchant,
                    list: [obj.list]
                }); // 创建新的对象
            }
            return acc;
        }, []);
        console.log(mergedArr); // 转换为数组格式输出结果
        this.setData({
            ids_list: val,
            mergedArr
        })
    },
    toOrder() {
        let mergedArr = JSON.stringify(this.data.mergedArr)
        wx.navigateTo({
            url: '/pages/shopping/order?mergedArr=' + mergedArr,
        })
    },
    toDel() {
        this.setData({
            delStatus: !this.data.delStatus
        })
    },
    onChange(e) {
        console.log(e)
        let index = e.currentTarget.dataset.index
        let index2 = e.currentTarget.dataset.index2
        let pid = e.currentTarget.dataset.pid
        let skuid = e.currentTarget.dataset.skuid
        let quantity = e.detail
        store.cart.update({
            product_id: pid,
            product_sku_id: skuid,
            quantity: quantity,
        }, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg,
                })
                this.setData({
                    ['list['+index+'].list['+index2+'].quantity']:quantity
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    // 删除
    toDelCom() {
        if (this.data.ids_list.length == 0) {
            wx.showToast({
                title: '请选择商品',
                icon: "none"
            })
            return false
        }
        let that = this
        let product_sku_id = this.data.ids_list.toString()
        wx.showModal({
            title: '提示',
            content: '确定要删除' + this.data.ids_list.length + '件商品？',
            complete: (res) => {
                if (res.cancel) {

                }
                if (res.confirm) {
                    store.cart.delete({
                        product_sku_id: product_sku_id
                    }, res => {
                        if (res.code == 1) {
                            wx.showToast({
                                title: res.msg,
                            })
                            that.getList()
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: "error"
                            })
                        }
                    })

                }
            }
        })
    },
    // 获取列表
    getList(page) {
        let that = this
        store.cart.myList({}, res => {
            res.data.forEach(item => {
                item.list.forEach(item2 => {
                    item2.checked = false
                })
            })
            that.setData({
                list: res.data
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        // pageNum1 = 1
        // this.setData({
        //     list: [],
        // });
        // this.getList(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        // pageNum1 += 1;
        // this.getList(pageNum1)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})